<?php

/**
 * 个人中心控制器
 * @author zhao
 * @date 17-04-12
 * @copyright (c) gandianli
 */

namespace app\index\controller;

use app\index\Base;
use think\Config;


class Index extends Base
{

    public function Index() 
    {
       return $this->view->fetch();
    }
    
    
    public function down()
    {
        header("Content-type:text/html;charset=utf-8");
        // $file_name="cookie.jpg"; 
        $file_name = "agk.apk";
        //用以解决中文不能显示出来的问题 
        $file_name = iconv("utf-8", "gb2312", $file_name);
        $file_sub_path = "/gdl/newexam/";
        $file_path = $file_sub_path . $file_name;
        //首先要判断给定的文件存在与否 
        if (!file_exists($file_path)) {
            echo "没有该文件文件";
            return;
        }
        $fp = fopen($file_path, "r");
        $file_size = filesize($file_path);
        //下载文件需要用到的头 
        Header("Content-type: application/octet-stream");
        Header("Accept-Ranges: bytes");
        Header("Accept-Length:" . $file_size);
        Header("Content-Disposition: attachment; filename=" . $file_name);
        $buffer = 1024;
        $file_count = 0;
        //向浏览器返回数据 
        while (!feof($fp) && $file_count < $file_size) {
            $file_con = fread($fp, $buffer);
            $file_count += $buffer;
            echo $file_con;
        }
        fclose($fp);
    }

    public function Ranking() 
    {
		
	$bascid = $this->request->param("bascid");
	$start = $this->request->param("start");
	$show = $this->request->param("show") ? $this->request->param("show") : 0;
	if(!$bascid){
		$bascid='256|255';
	}
	if(!$start){
		$start='2017-09-20';
	}
	$starttime = strtotime($start);
	$basc_arr = array();
	foreach(explode('|',$bascid) as $v){
		if(!$v) break;
		$basc_arr[] = (int)$v;
	}
	if(empty($basc_arr)){
		return '没有排名';
	}
	
	
      $Basic = new \app\common\model\ExamsPapers(); //identity_id
	  $seniority =  $Basic->alias('a')->join('ex_papers_info b','a.ep_infoId= b.id')->join("user_company d","a.identity_id = d.id",'left')->join("user c","d.userid = c.userid",'left')->field('a.ep_paper_name,b.score,b.time,c.realname,c.idcard,a.basicid,b.starttime,d.company_name,c.phone')->where('a.isvalid=1 and a.ep_type=2 and a.basicid in ('.implode(',',$basc_arr).' )  and b.starttime>'.$starttime)->group('c.userid')->order('b.score desc,b.time asc,b.starttime asc')->limit(100)->select();
	  $senc = array();
	  $i=1;
	 foreach($seniority as $k=>$v){
		 if($v->realname=='') continue;
		 $senc[] = [
			'k'=>$i,
			'realname'=>$v->realname,
			'realname_r'=>mb_substr($v->realname,0,1,'utf-8').'**',
			'idcard'=>$v->idcard ? substr($v->idcard,0,6).'***'.substr($v->idcard,14,strlen($v->idcard)) : $v->idcard,
			'score'=>$v->score,
			'dtime'=>secsToStrws($v->time),
			'company_name'=>$v->company_name,
			'phone'=>$v->phone,
			];
		 
		
		$i++;
	}

		$this->view->assign('seniority', $senc);
		$this->view->assign('show', $show);
		return $this->view->fetch();
    }
	
	

}
